package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.qdlc.p2p.dal.common.SystemConfigHelper;
import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.ArticleService;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.dao.ArticleDao;
import com.qdlc.p2p.dal.dao.ArticleRankDao;
import com.qdlc.p2p.dal.dao.SiteDao;
import com.qdlc.p2p.dal.dto.Article;
import com.qdlc.p2p.dal.dto.Site;
import com.qdlc.p2p.dal.model.ArticleModel;
import com.qdlc.p2p.dal.model.SiteTree;
import com.qdlc.p2p.dal.model.Tree;

@Service("articleService")
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleDao articleDao;

    @Resource
    private SiteDao siteDao;

    @Resource
    private ArticleRankDao articleRankDao;

    @Override
    public List<ArticleModel> listBySize(String nid, int size) {
        return articleDao.listBySize(nid, size);
    }

    @Override
    public PageDataList<ArticleModel> list(String nid, int page) {
        return articleDao.list(nid, page);
    }

    @Override
    public PageDataList<ArticleModel> list(ArticleModel model) {
        QueryParam param = QueryParam.getInstance();
        param.addParam("site.nid", model.getNid());
        param.addParam("status", 1);
        param.addOrder(OrderType.DESC, "isTop");
        param.addOrder(OrderType.DESC, "isRecommend");
        param.addOrder(OrderType.ASC, "sort");
        param.addOrder(OrderType.DESC, "id");
        param.addPage(model.getPage(), model.getSize());
        PageDataList<Article> pageDataList = articleDao.findPageList(param);
        PageDataList<ArticleModel> pageDataList_ = new PageDataList<ArticleModel>();
        List<ArticleModel> list = new ArrayList<ArticleModel>();
        List<Article> articles = pageDataList.getList();
        pageDataList_.setPage(pageDataList.getPage());
        if (articles.size() > 0) {
            for (int i = 0; i < articles.size(); i++) {
                Article an = articles.get(i);
                ArticleModel anm = ArticleModel.instance(an);
                anm.setNid(an.getSite().getNid());
                list.add(anm);
            }
        }
        pageDataList_.setList(list);
        return pageDataList_;
    }

    @Override
    public PageDataList<ArticleModel> articleList(ArticleModel model, int pageNumber, int pageSize) {
        return articleDao.articleList(model, pageNumber, pageSize);
    }

    @Override
    public void articleAdd(Article article) {
        articleDao.save(article);
    }

    @Override
    public Article find(long id) {
        return articleDao.find(id);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public SiteTree getSiteTree() {
        List<Site> list = siteDao.list();
        SiteTree tree = new SiteTree(null, new ArrayList<Tree>());
        for (Site s : list) {
            SiteTree secTree = new SiteTree(s, new ArrayList<Tree>());
            List<Site> sublist = siteDao.list(s.getId(), s.getStatus());
            for (Site ss : sublist) {
                SiteTree subTree = new SiteTree(ss, null);
                secTree.addChild(subTree);
            }
            tree.addChild(secTree);
        }
        return tree;
    }

    @Override
    public void update(Article article) {
        articleDao.merge(article);
    }

    @Override
    public List<Long> getIds(Site site) {
        List<Long> longs = new ArrayList<Long>();
        List<Object> list = articleDao.getIds(site);
        for (Object o : list) {
            longs.add((long) ((Integer) o));
        }
        return longs;
    }

    @Override
    public PageDataList<ArticleModel> rankList(String type) {
        PageDataList<ArticleModel> pageDataList = new PageDataList<ArticleModel>();
        List<Article> articleList = articleRankDao.rankList(type);

        List<ArticleModel> articleModelList = new ArrayList<ArticleModel>();
        if (!articleList.isEmpty() && articleList.size() > 0) {
            for (Article article : articleList) {
                ArticleModel model = ArticleModel.instance(article);
                model.setNid(model.getSite().getNid());
                articleModelList.add(model);
            }
        }
        pageDataList.setList(articleModelList);
        return pageDataList;
    }
    /**
     * 公告栏列表(包括)
     * @param size
     * @return
     */
    @Override
    public List<ArticleModel> listNotice(int size,Object... value) {
        QueryParam param = QueryParam.getInstance();
        //param.addParam("site.nid", model.getNid());
        param.addOrFilter("site.nid",value);
        param.addParam("status", 1);
        param.addOrder(OrderType.DESC, "isTop");
        param.addOrder(OrderType.DESC, "isRecommend");
        param.addOrder(OrderType.ASC, "sort");
        param.addOrder(OrderType.DESC, "id");
        List<Article> list = articleDao.findByCriteria(param, 0, size);
        List<ArticleModel> mlist = new ArrayList<ArticleModel>();
        for (Article articleNew : list) {
            ArticleModel anm = ArticleModel.instance(articleNew);
            anm.setNid(articleNew.getSite().getNid());
            anm.setPicPath(SystemConfigHelper.getValue(SystemConfigHelper.Nid.IMAGE_SERVER_URL) + articleNew.getPicPath());
            anm.setSite(null);
            mlist.add(anm);
        }
        return mlist;
    }

}
